package com.lizi.textView;
import android.app.AlertDialog;
import android.os.Bundle;
import android.app.Activity;
import android.view.accessibility.AccessibilityManager;
import android.widget.TextView;
import android.view.View;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import android.widget.Button;
import android.widget.DatePicker;
import android.view.View.OnClickListener;
import android.app.DatePickerDialog;
import android.widget.Toast;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.widget.ProgressBar;
public class DatePickerDialogExample extends Activity {
	private TextView showdate;
	private Button setdate;
	private int year;
	private int month;
	private int day;
	private int yearCur;
	private int monCur;
	private int dayCur;
	private ProgressBar firstBar=null;
	private ProgressBar secondBar=null;
	private Button myButton=null;
	private int i=0;
	    @Override
	    protected void onCreate(Bundle savedInstanceState)
	    {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);
	        Builder dialog=new AlertDialog.Builder(DatePickerDialogExample.this);
			dialog.setTitle("Welcome");
			dialog.setMessage("Welcome to my program");
			dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			dialog.create();
			dialog.show();
			firstBar=(ProgressBar)findViewById(R.id.bar1);
			secondBar=(ProgressBar)findViewById(R.id.bar2);
			myButton=(Button)findViewById(R.id.btn);
			myButton.setOnClickListener(new ButtonListener());
	        showdate=(TextView) this.findViewById(R.id.showtime);
	        setdate=(Button) this.findViewById(R.id.setdate);
	        //初始化Calendar日历对象
	        Calendar c=Calendar.getInstance(Locale.CHINA);
	        Date mydate=new Date(); //获取当前日期Date对象
	        c.setTime(mydate);////为Calendar对象设置时间为当前日期
	        year=c.get(Calendar.YEAR); //获取Calendar对象中的年
	        month=c.get(Calendar.MONTH);//获取Calendar对象中的月
	        day=c.get(Calendar.DAY_OF_MONTH);//获取这个月的第几天
			yearCur=year;
			monCur=month;
			dayCur=day;
	        showdate.setText("当前日期:"+year+"-"+(month+1)+"-"+day); //显示当前的年月日
	        //添加单击事件--设置日期
	        setdate.setOnClickListener(new OnClickListener(){
	            
	            @Override
	            public void onClick(View v)
	            {
	                //创建DatePickerDialog对象
	                DatePickerDialog my_datePickerDialog=new DatePickerDialog(DatePickerDialogExample.this,Datelistener,year,month,day);
	                my_datePickerDialog.show();//显示DatePickerDialog组件
	            }
	        });
	    }
	    private DatePickerDialog.OnDateSetListener Datelistener=new DatePickerDialog.OnDateSetListener()
	    {
	        @Override
	        public void onDateSet(DatePicker v, int y, int m,int d) {
	            //修改year、month、day的变量值，以便以后单击按钮时，DatePickerDialog上显示上一次修改后的值
				if(y<yearCur||(y==yearCur&&m<monCur)||(y==yearCur&&m==monCur&&d<=dayCur)) {
					year = y;
					month = m;
					day = d;
					//更新日期
					updateDate();
				}else{
					Toast.makeText(DatePickerDialogExample.this,"Wrong date",Toast.LENGTH_LONG).show();
				}
	        }
	        //当DatePickerDialog关闭时，更新日期显示
	        private void updateDate()
	        {
	            //在TextView上显示日期
	            showdate.setText("当前日期："+year+"-"+(month+1)+"-"+day);
	        }
	    };
	    class ButtonListener implements OnClickListener{
			public void onClick(View v){
				if(i==0){
					firstBar.setVisibility(View.VISIBLE);
					secondBar.setVisibility(View.VISIBLE);
					i+=10;
					firstBar.setMax(150);
				}else if(i<firstBar.getMax()){
					firstBar.setProgress(i);
					i+=10;
					firstBar.setSecondaryProgress(i);
				}else {
					i=0;
					firstBar.setProgress(i);
					firstBar.setVisibility(View.GONE);
					secondBar.setVisibility(View.GONE);
				}
			}
		}
    
}